//
//  ShopDetailsViewController.m
//  大轮胎
//
//  Created by 朱永超 on 2017/3/29.
//  Copyright © 2017年 zyc. All rights reserved.
//

#import "ShopDetailsViewController.h"
//引入头文件
#import "YCNetWork.h"
#import "ShopDetailsModel.h"
#import "ShopDetailsTableViewCell.h"
#import <UITableView+FDTemplateLayoutCell.h>
#import <Masonry.h>
#import "ShopDetailsHeaderView.h"
#import <SDWebImage/UIImageView+WebCache.h>
@interface ShopDetailsViewController ()
{
    NSMutableArray *shopDeArr;
    ShopDetailsHeaderView *theHeaderView;  //表格头视图
}
@end

@implementation ShopDetailsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"详情";
    //导航左按钮
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"left"] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
    self.navigationController.navigationBar.tintColor = [UIColor whiteColor];
    
    //获取商铺详情数据
    [self shopNetWorking];
    
    //注册表格
    [self.tableView registerClass:[ShopDetailsTableViewCell class] forCellReuseIdentifier:@"shopCell"];
    self.tableView.sectionHeaderHeight = 40;
    
}
//获取商铺详情数据
-(void)shopNetWorking
{
    [YCNetWork postWithURLString:self.urlStr parameters:nil success:^(id data) {
        //存储简介和联系人的数据
        shopDeArr = [NSMutableArray array];
        ShopDetailsModel *shopDeMo = [[ShopDetailsModel alloc] init];
        [shopDeMo setValuesForKeysWithDictionary:data];
        [shopDeArr addObject:shopDeMo];
        [self headImageV:shopDeMo.logo titleLabel:shopDeMo.name];
        [self.tableView reloadData];
    } failure:^(NSError *error) {
        
        
    }];
}
//返回按钮
-(void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}
-(void)headImageV:(NSString *)imageStr titleLabel:(NSString *)titleStr
{
    theHeaderView = [[ShopDetailsHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, (self.view.frame.size.width+40)/3)];
    [theHeaderView.headImageView sd_setImageWithURL:[NSURL URLWithString:imageStr] placeholderImage:[UIImage imageNamed:@""]];
    theHeaderView.titleLabel.text = titleStr;
    self.tableView.tableHeaderView = theHeaderView;
    
}
#pragma mark - UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return shopDeArr.count;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ShopDetailsTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"shopCell"];
    ShopDetailsModel *shopModel = shopDeArr[indexPath.row];
    cell.shopDeModel = shopModel;
    
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    //自适应高度
    return [tableView fd_heightForCellWithIdentifier:@"shopCell" configuration:^(ShopDetailsTableViewCell *cell) {
        ShopDetailsModel *shopM = shopDeArr[indexPath.row];
        cell.shopDeModel = shopM;
    }];
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 40)];
    NSArray *arr = @[@"简介",@"产品",@"联系"];
    UISegmentedControl *segmentC = [[UISegmentedControl alloc] initWithItems:arr];
    segmentC.frame = CGRectMake(10, 0, self.view.frame.size.width-20, 40);
    [view addSubview:segmentC];
    return view;
}
@end
